﻿using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using DataBaseReverseGenerator.DataBaseModelGenerator;
using DataBaseReverseGenerator.DataBaseModel;

namespace DbReverseGeneratorTest
{
    [TestFixture]
    public class DbExplorerTest
    {
        private DataBaseExplorer dbExplorer;

        public DbExplorerTest()
        {
            dbExplorer = new DataBaseExplorer();
            dbExplorer.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=AdventureWorksLT;Data Source=.";
            //dbExplorer.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\..\cpr.mdb;Persist Security Info=False";
        }

        #region Utils

        private void DisplayModel(DataBase dbModel)
        {
            Console.WriteLine("DataBase Name={0}", dbModel.Name);
            foreach (Table t in dbModel.Tables)
            {
                Console.WriteLine("\tTable Name={0}", t.Name);

                if(t.PrimaryKeys.Count > 0)
                {
                    Console.WriteLine("\t\tPrimaryKeys={0}", String.Join(",", t.PrimaryKeys.ToArray()));
                }

                foreach(Column c in t.Columns)
                {
                    Console.WriteLine("\t\tColumn Name={0} \tType={1} \tSize={2} \tIsIdentity={3}", c.Name, c.Type, c.Size, c.IsIdentity);
                }
            }

            foreach (RelationShip rel in dbModel.RelationShips)
            {
                Console.WriteLine("\tRelationship ParentTable={0} \tChildTable={1}", rel.ParentTable, rel.ChildTable);
                
                foreach (Rule rule in rel.Rules)
                {
                    Console.WriteLine("\t\tRule ParentField={0} \tChildField={1}", rule.ParentField, rule.ChildField);
                }
            }
        }

        #endregion

        [Test]
        public void ExploreDatabase()
        {
            dbExplorer.Explore();
            DisplayModel(dbExplorer.DbModel);
        }

        [Test]
        public void SerializeDbModel()
        {
            dbExplorer.Explore();
            dbExplorer.DbModel.Serialize("dbModel.xml");
        }
    }
}
